package com.starblink.library.widget.novice_guide.camera

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.RectF
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.android.basic.databinding.LayoutPopBaseHollowBinding
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.android.basic.util.PopupWindowUtils
import com.starblink.library.widget.databinding.AddVImageSearchBinding

object ImgSearchPopView {
    var pp: PopupWindow? = null
    val downTimer = FlowCountDownTimer(time = 3)

    @SuppressLint("ClickableViewAccessibility")
    inline fun pushPopWindow(
        context: Activity,
        anchorView: View,
        parent: ViewGroup,
        crossinline dismissCallBack: () -> Unit
    ) {
        GuidePopStatusControl.getHollowRectF(anchorView, true)?.let { rectF ->

            val bd =
                LayoutPopBaseHollowBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            bd.popBaseHollow.setRect(RectF(0f, 0f, 0f, 0f), 0f)
            //↓
            val toastLay =
                AddVImageSearchBinding.inflate(LayoutInflater.from(context), bd.root, false)

            //↑
            bd.root.addView(toastLay.root)

            val pp = PopupWindowUtils.getNormalPopWindow(
                context = context,
                anchorView = bd.root,
                parent = parent,
                anchorRect = null,
                dismissClickView = toastLay.root,
                dismissCallBack = {
                    SpmTrackHandler.addSingleTrackData(
                        SkAntEntity(
                            spmCnt = GTrackerAssistUtils.fetchElementValue(
                                SpmPageDef.Page_10207, SpmElementDef.Element_33060
                            )
                        )
                    )
                    dismissCallBack.invoke()
                    downTimer.cancel()

                })

            pp.setOnBackPressListener {
                //拦截物理返回键
                dismissCallBack.invoke()
                downTimer.cancel()
            }

            SpmTrackHandler.addSingleTrackData(
                SkAntEntity(
                    event = TrackEvent.pageExposure.value,
                    spmCnt = GTrackerAssistUtils.fetchPageValue(
                        SpmPageDef.Page_10207
                    )
                )
            )

//            toastLay.dismissLay.click {
//                pp.dismiss()
//                dismissCallBack.invoke()
//                downTimer.cancel()
//            }

            downTimer.start(callBack = {
            }, onFinish = {
                pp.dismiss()
            })
        }
    }

}